<?php
namespace app;
use think\DB;
use think\Request;
    $request = Request::instance();
    $settings = db('BaseSettings')->select();
    foreach ($settings as $k => $v) {
        $settingInfo[$v['key']] = $v['val'];
    }
    unset($settings);
    $rewrite = $settingInfo['rewrite'] ? '' : '/index.php?s=';
    $domain = $request->domain() . $rewrite;
    if ($settingInfo['domain']) {
        $domain = $settingInfo['httpType'] . $settingInfo['domain'] . $rewrite;
    }
    if ($settingInfo['runUrl']) {
        $domain =  $settingInfo['runUrl'];
        if (strpos($domain{(strlen(trim($domain))-1)},'/') !== false) {
            $domain = substr($domain,0,strlen($domain)-1); 
        }
        $domain =  $domain . $rewrite;
    }
    
    $domainInfo = db('BaseDomain')->where('domain',$request->server()['HTTP_HOST'])->find();
    if ($domainInfo) {
        $domain = $domainInfo['http_type'] . $domainInfo['domain'];
        if ($domainInfo['data']) {
            $domainData = json_decode($domainInfo['data'],true);
            if ($domainData) {
                foreach ($domainData as $key => $value) {
                    if ($value) {
                        $settingInfo[$key] = $value;
                    }
                }
            }
        }
    }
    
    $domainStatic = str_replace('/index.php?s=', '', $domain);
    config('domainStatic',$domainStatic);
    $domains = $domainStatic . '/index.php?s=';
    config('domains',$domains);
    config('domain',$domain);
    config('settingInfo',$settingInfo);
    
    $tplName = config('settingInfo')['template'];
    if ($request->isMobile()) {
        if ($settingInfo['superTpl']) {
            $tplName = 'm';
        }
    }
    if (strpos($request->server('QUERY_STRING'),config('admin')) === false) {
        if ($settingInfo['codeCompression']) {
            config('template.strip_space',true);
        }
    }
    unset($settingInfo);
    
    if (db('BaseAddons')->where('name','spidertpl')->find()) {
        if (isSpider()) {
            $tplName = 'spider_' . $tplName;
        }
    }
    $tplName = $tplName ? $tplName : 'default';
    config('tplName',$tplName);
    setTemplateConfigPath($tplName);
    
    